﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Diagnostics;
using System.IO.Ports;

using System.Threading;
using System.Text.RegularExpressions;
namespace call2sms
{
    class CallMonitor
    {

        GSMPort port;
      

        public GSMPort Port
        {
            get { return port; }
        }

        private string lastCaller;

        
        public delegate void onCall(string number);

        public onCall onCallHandler
        {
            set;
            private get;
        }
        public CallMonitor()
        {
           
        }

        ~CallMonitor()
        {
            Stop();
        }

        public void Stop()
        {
          
          //  port.Close();
            port.Stop();

        }

        public bool IsRunning
        {
            get 
            { 
                return ((port != null) && port.IsRunning); 
            }
        }

        public void OnMessage(string msg)
        {
            Match match = Regex.Match(msg, "\\+CLIP:\\s*\"([0-9]+)\",.*");
            if (match.Success)
            {
                string phone = match.Groups[1].Value;
               // if (phone != lastCaller)
                {
                    lastCaller = phone;
                    if (onCallHandler != null) onCallHandler(phone);
                }
            }
        }


        public void Start(string com)
        {
            try
            {
                port = new GSMPort(com);

                port.Init();
                String result = port.ExecCommand("AT+CLIP=1", 500, "error +CLIP=1");

                port.WorkMode = GSMPort.GSMPortWorkMode.ThreadLoopMode;
                port.PartialMsgReceived += OnMessage;
                port.Start();
            }
            catch (Exception e)
            {
                port.Close();
                throw e;
            }
            //Thread thread = new Thread(new ThreadStart(this.MonitorThread));
            //thread.Start();
        }
        /*private void MonitorThread()
        {
            running = true;
            String result = port.ExecCommand("AT+CLIP=1", 300, "error +CLIP=1");
            while (running)
            {
                try
                {
                    String msg = port.ReadResponse(Timeout.Infinite, false);
                    //+CLIP: "0xx67848994",129
                    if (msg.IndexOf("RING") >= 0)
                    {
                    }
                    else
                    {
                        Match match = Regex.Match(msg, "\\+CLIP:\\s*\"([0-9]+)\",.*");
                        if (match.Success)
                        {
                            string phone = match.Groups[1].Value;
                            if (phone != lastCaller)
                            {
                                lastCaller = phone;
                                if (onCallHandler != null) onCallHandler(phone);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.Write(e);
                }          
            }            
        }*/
    }
}
